/* 
 * File:   main.cpp
 * Author: steve
 *
 * Created on 16 January 2013, 21:00
 */

#include <cstdlib>
#include <iostream>
#include <vector>
#include "point.h"

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkTetra.h>
#include <vtkCellArray.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkUnstructuredGrid.h>
#include <vtkPointData.h>
#include <vtkVertexGlyphFilter.h>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    
    // Generate a list of points
    vector<point>  mypoints(10);
    
    cout << mypoints.size() << endl;

    if(argc != 2) {
        std::cout << "Required arguments: OutputFilename" << std::endl;
        return EXIT_FAILURE;
    }
    std::string filename = argv[1];

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(0.0, 0.0, 0.0);
    points->InsertNextPoint(1.0, 0.0, 0.0);
    points->InsertNextPoint(0.0, 1.0, 0.0);
    points->InsertNextPoint(0.0, 0.0, 1.0);
    points->InsertNextPoint(1.0, 0.0, 1.0);
    points->InsertNextPoint(1.0, 1.0, 1.0);

    vtkSmartPointer<vtkTetra> tetra1 = vtkSmartPointer<vtkTetra>::New();
    tetra1->GetPointIds()->SetId(0, 0);
    tetra1->GetPointIds()->SetId(1, 1);
    tetra1->GetPointIds()->SetId(2, 2);
    tetra1->GetPointIds()->SetId(3, 3);
    vtkSmartPointer<vtkCellArray> cellArray = vtkSmartPointer<vtkCellArray>::New();

    vtkSmartPointer<vtkTetra> tetra2 = vtkSmartPointer<vtkTetra>::New();
    tetra2->GetPointIds()->SetId(0, 1);
    tetra2->GetPointIds()->SetId(1, 3);
    tetra2->GetPointIds()->SetId(2, 4);
    tetra2->GetPointIds()->SetId(3, 5);

    cellArray->InsertNextCell(tetra1);
    cellArray->InsertNextCell(tetra2);

    vtkSmartPointer<vtkUnstructuredGrid> unstructuredGrid =
        vtkSmartPointer<vtkUnstructuredGrid>::New();
    unstructuredGrid->SetPoints(points);
    unstructuredGrid->SetCells(VTK_TETRA, cellArray);

    // Write file
    vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
        vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();

    writer->SetFileName(filename.c_str());

#if VTK_MAJOR_VERSION <= 5
    writer->SetInput(unstructuredGrid);
#else
    writer->SetInputData(unstructuredGrid);
#endif

    writer->Write();

    // Read and display file for verification that it was written correclty
    vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
        vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
    reader->SetFileName(filename.c_str());
    reader->Update();

    vtkSmartPointer<vtkDataSetMapper> mapper =
            vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(.3, .6, .3); // Background color green

    renderWindow->Render();
    renderWindowInteractor->Start();

    return 0;
}

